package com.lframework.xingyun.promo.controller;

import com.lframework.starter.web.core.annotations.security.HasPermission;
import com.lframework.starter.web.core.controller.DefaultBaseController;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.components.resp.InvokeResult;
import javax.validation.constraints.NotBlank;

import com.lframework.xingyun.promo.bo.fullgiftactivity.GetMarketingFullgiftactivityBo;
import com.lframework.xingyun.promo.bo.fullgiftactivity.QueryMarketingFullgiftactivityBo;
import com.lframework.xingyun.promo.entity.MarketingFullgiftactivity;
import com.lframework.xingyun.promo.service.fullgiftactivity.MarketingFullgiftactivityService;
import com.lframework.xingyun.promo.vo.fullgiftactivity.CreateMarketingFullgiftactivityVo;
import com.lframework.xingyun.promo.vo.fullgiftactivity.QueryMarketingFullgiftactivityVo;
import com.lframework.xingyun.promo.vo.fullgiftactivity.UpdateMarketingFullgiftactivityVo;
import io.swagger.annotations.ApiImplicitParam;
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import io.swagger.annotations.ApiOperation;
import com.lframework.starter.common.utils.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 限时满赠活动表 Controller
 *
 */
@Api(tags = "限时满赠活动表")
@Validated
@RestController
@RequestMapping("/promo/fullgiftactivity")
public class MarketingFullgiftactivityController extends DefaultBaseController {

    @Autowired
    private MarketingFullgiftactivityService marketingFullgiftactivityService;

    /**
     * 查询列表
     */
    @ApiOperation("查询列表")
    @HasPermission({"promotions:fullgiftactivity:query"})
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryMarketingFullgiftactivityBo>> query(@Valid QueryMarketingFullgiftactivityVo vo) {

        PageResult<MarketingFullgiftactivity> pageResult = marketingFullgiftactivityService.queryPage(getPageIndex(vo), getPageSize(vo), vo);

        List<MarketingFullgiftactivity> datas = pageResult.getDatas();
        List<QueryMarketingFullgiftactivityBo> results = null;

        if (!CollectionUtil.isEmpty(datas)) {
            results = datas.stream().map(QueryMarketingFullgiftactivityBo::new).collect(Collectors.toList());
        }

        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult, results));
    }

    /**
     * 根据ID查询
     */
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(value = "id", name = "id", paramType = "query", required = true)
    @HasPermission({"promotions:fullgiftactivity:query"})
    @GetMapping
    public InvokeResult<GetMarketingFullgiftactivityBo> get(@NotBlank(message = "id不能为空！") String id) {

        MarketingFullgiftactivity data = marketingFullgiftactivityService.findById(id);
        if (data == null) {
            throw new DefaultClientException("限时满赠活动表不存在！");
        }

        GetMarketingFullgiftactivityBo result = new GetMarketingFullgiftactivityBo(data);

        return InvokeResultBuilder.success(result);
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @HasPermission({"promotions:fullgiftactivity:add"})
    @PostMapping
    public InvokeResult<Void> create(@Valid @RequestBody CreateMarketingFullgiftactivityVo vo) {

        marketingFullgiftactivityService.create(vo);

        return InvokeResultBuilder.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @HasPermission({"promotions:fullgiftactivity:modify"})
    @PutMapping
    public InvokeResult<Void> update(@Valid @RequestBody UpdateMarketingFullgiftactivityVo vo) {

        marketingFullgiftactivityService.update(vo);

        marketingFullgiftactivityService.cleanCacheByKey(vo.getId());

        return InvokeResultBuilder.success();
    }
}

